package com.ggs.rg.ybjb.usermodule.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ggs.rg.ybjb.pojomodule.pojo.Goods;
import com.ggs.rg.ybjb.pojomodule.pojo.Mater;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.usermodule.service.GoodsService;
import com.ggs.rg.ybjb.usermodule.service.MaterService;
import com.ggs.rg.ybjb.usermodule.util.ImageUtil;
import com.ggs.rg.ybjb.usermodule.util.ListPagesUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * ClassName:TGoodsController
 * Package:com.ggs.rg.ybjb.usermodule.controller
 * Description:
 *
 * @Date:2021/11/26 8:53
 * @Author:cxw
 */
@RestController
@RequestMapping("/goods")
public class TGoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    MaterService materService;

    //首页分类展示商品
    @GetMapping("/getGoodsByType")
    public JSONObject getGoodsByType(@RequestParam("type") String type,
                                     @RequestParam(value = "pagesNumber",required = false) Integer pagesNumber){
        if (StringTools.isNullOrEmpty(pagesNumber)){
            pagesNumber = 1;
        }else if (pagesNumber <= 0){
            pagesNumber = 1;
        }
        List<Map<String,Object>> mapList = new ArrayList<>();
        if (!StringTools.isNullOrEmpty(type)){
            QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
            //倒序输出，orderByDesc
            QueryWrapper<Goods> select = queryWrapper.eq("type", type).orderByDesc("create_time");
            List<Goods> list = goodsService.list(select);
            for (Goods g : list){
                Map<String,Object> goodsMap = new TreeMap<>();
                List<Double> priceList = new ArrayList<>();
                int gid = g.getGid();
                String name = g.getName();
                String image = g.getImage();
                String imageAddr = ImageUtil.getImageAddr(image, "9001");
                QueryWrapper<Mater> queryWrapper1 = new QueryWrapper<>();
                QueryWrapper<Mater> select1 = queryWrapper1.eq("gid", gid).select("price");
                List<Mater> list1 = materService.list(select1);
                for (Mater m : list1){
                    Double price = m.getPrice();
                    priceList.add(price);
                }
                //最小
                Double min = Collections.min(priceList);
                goodsMap.put("gid",gid);
                goodsMap.put("name",name);
                goodsMap.put("image",imageAddr);
                goodsMap.put("priceMin",min);
                mapList.add(goodsMap);
            }
            Map<String, Object> map = ListPagesUitl.listPaging(mapList, pagesNumber, 16);
            return CommonUtil.successJson(map);
        }else {
            return CommonUtil.successJson();
        }
    }

    //首页搜索倒叙输出
    @GetMapping("/searchGoodsByName")
    public JSONObject searchGoodsByName(@RequestParam("name") String name,
                                        @RequestParam(value = "pagesNumber",required = false) Integer pagesNumber){
        if (StringTools.isNullOrEmpty(pagesNumber)){
            pagesNumber = 1;
        }else if (pagesNumber <= 0){
            pagesNumber = 1;
        }
        List<Map<String,Object>> mapList = new ArrayList<>();
        if (StringTools.isNullOrEmpty(name)){
            return CommonUtil.successJson();
        }else {
            QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
            //倒序输出，orderByDesc
            QueryWrapper<Goods> select = queryWrapper.like("name", name).orderByDesc("create_time");
            List<Goods> list = goodsService.list(select);
            for (Goods g : list){
                Map<String,Object> goodsMap = new TreeMap<>();
                List<Double> priceList = new ArrayList<>();
                int gid = g.getGid();
                String image = g.getImage();
                String imageAddr = ImageUtil.getImageAddr(image, "9001");
                String name1 = g.getName();
                QueryWrapper<Mater> queryWrapper1 = new QueryWrapper<>();
                QueryWrapper<Mater> select1 = queryWrapper1.eq("gid", gid).select("price");
                List<Mater> list1 = materService.list(select1);
                for (Mater m : list1){
                    Double price = m.getPrice();
                    priceList.add(price);
                }
                //最小
                Double min = Collections.min(priceList);
                goodsMap.put("gid",gid);
                goodsMap.put("name",name1);
                goodsMap.put("image",imageAddr);
                goodsMap.put("priceMin",min);
                mapList.add(goodsMap);
            }
        }
        Map<String, Object> map = ListPagesUitl.listPaging(mapList, pagesNumber, 16);
        return CommonUtil.successJson(map);
    }
}
